package com.jxstjh.conch.common.vo;

import com.jxstjh.conch.common.enums.ResponseStatusEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * 返回消息结构体
 */
@Getter
@ToString(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
public final class BasicResultVO<T> {

    /**
     * 返回状态
     */
    private String status;

    /**
     * 返回描述
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;

    public BasicResultVO(ResponseStatusEnum status, String msg, T data) {
        this.status = status.getCode();
        this.msg = msg;
        this.data = data;
    }

    public BasicResultVO(ResponseStatusEnum status) {
        this(status, null);
    }

    public BasicResultVO(ResponseStatusEnum status, T data) {
        this(status, status.getMsg(), data);
    }

    /**
     * 默认成功返回
     */
    public static BasicResultVO<Void> success() {
        return new BasicResultVO<>(ResponseStatusEnum.SUCCESS);
    }

    /**
     * 自定义成功返回
     */
    public static <T> BasicResultVO<T> success(String msg) {
        return new BasicResultVO<>(ResponseStatusEnum.SUCCESS, msg, null);
    }

    /**
     * 带数据的成功返回
     */
    public static <T> BasicResultVO<T> success(T data) {
        return new BasicResultVO<>(ResponseStatusEnum.SUCCESS, data);
    }

    /**
     * 默认失败返回响应
     */
    public static <T> BasicResultVO<T> fail() {
        return new BasicResultVO<>(ResponseStatusEnum.FAIL, ResponseStatusEnum.FAIL.getMsg(), null);
    }

    /**
     * 自定义状态和信息的失败响应
     */
    public static <T> BasicResultVO<T> fail(ResponseStatusEnum status, String msg) {
        return new BasicResultVO<>(status, msg, null);
    }

    /**
     * 自定义错误信息的失败响应
     */
    public static <T> BasicResultVO<T> fail(String msg) {
        return fail(ResponseStatusEnum.FAIL, msg);
    }

    /**
     * 自定义状态的失败响应
     */
    public static <T> BasicResultVO<T> fail(ResponseStatusEnum status) {
        return fail(status, status.getMsg());
    }
}